package com.hyt.it.ogt.kq.main.controller.bm;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.base.BaseCheckEntity;
import com.hyt.core.validate.Check;
import com.hyt.core.validate.Insert;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.exception.exception.BusinessLogicException;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.common.config.TemplateFileConfig;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.gov.config.swagger.ApiVersionConstant;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.service.bm.model.param.NonOpenInfoSaveParam;
import com.hyt.it.ogt.kq.service.bm.model.vo.NoOpenParam;
import com.hyt.it.ogt.kq.service.bm.model.vo.NoOpenQueryParam;
import com.hyt.it.ogt.kq.service.bm.model.vo.NonOpenInfoVO;
import com.hyt.it.ogt.kq.service.bm.service.INonOpenService;
import com.hyt.it.ogt.kq.service.gov.model.param.*;
import com.hyt.it.ogt.kq.service.gov.model.vo.BatchSelectStudentMessageVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.ImportStudentQualificationsCheckVO;
import com.hyt.log.annotation.Log;
import com.hyt.loginfo.controller.BaseLoginController;
import com.hyt.progress.creator.anno.ProgressAnno;
import com.hyt.progress.service.ProgressManager;
import com.hyt.swagger.ApiVersion;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;

/**
 * 非开放性报名接口
 *
 * @author wangt
 * @Description:
 * @date: 2021年1月19日 上午10:05:10
 * @version: V1.0
 */
@Slf4j
@RestController
@Api(tags = "21.非开放性报名", value = "非开放性报名")
@ApiSort(value = 21)
@RequestMapping("/bm/nonOpen")
public class NonOpenController extends BaseLoginController {

    @Resource
    INonOpenService iNonOpenService;
    @Resource
    TemplateFileConfig templateFileConfig;

    @ApiOperation(value = "21.1.1 查看非开放性报名列表", notes = "20000:成功；31211101：查看非开放性报名列表异常；")
    @ApiOperationSort(value = 1)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31211101, print = "查看非开放性报名列表异常")
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ApiResponse<IPage<NonOpenInfoVO>> query(NoOpenParam projectParam) {
        if (StrUtil.isBlank(projectParam.getProjectId())) {
            BusinessLogicException.throwException(31211102, "查看非开放性报名列表缺少报名项目");
        }
        
        projectParam.setDeptIds(getDeptIds());
        IPage<NonOpenInfoVO> page = iNonOpenService.queryPage(projectParam);
        return ApiResponse.<IPage<NonOpenInfoVO>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(page)
                .build();
    }

    @ApiOperation(value = "21.1.2 新增/编辑-非开放报名考生",
            notes = "20000:成功；31211201：新增/编辑-非开放报名考生异常；31211202：身份证不合法；" +
                    "31211203：身份证号码已存在；31211204：保存失败，请重试；")
    @ApiOperationSort(value = 2)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31211201, print = "新增/编辑-非开放报名考生异常")
    @Log(module = "非开放性报名管理", business = "非开放报名考生操作")
    @RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
    public ApiResponse<?> saveOrUpdateStudent(@Validated @RequestBody NonOpenInfoSaveParam param,
                                              BindingResult bindingResult) {
        this.checkValid(bindingResult);

        iNonOpenService.saveOrUpdateStudent(param);
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .build();
    }

    @ApiOperation(value = "21.1.3 下载非开放考生报名模板", notes = "20000:成功")
    @ApiOperationSort(value = 3)
    @GetMapping(value = "/downStudentDemo")
    public ApiResponse<String> downStudentDemo() {
        return ApiResponse.<String>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(templateFileConfig.getStudentQualifications())
                .build();
    }

    @ApiOperation(value = "21.1.5 删除考生", notes = "20000:成功")
    @ApiOperationSort(value = 5)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31211501, print = "批量删除考生失败")
    @Log(module = "非开放性报名管理", business = "删除考生")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ApiResponse<?> delete(String studentId) {
        iNonOpenService.batchDeleteStudent(studentId);
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data("删除考生成功！")
                .build();
    }

    @ApiOperation(value = "21.2.1 批量选择考生校验",
            notes = "20000:成功",
            response = BatchSelectStudentMessageVO.class)
    @ApiOperationSort(value = 6)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ProgressAnno(timeoutSeconds = 60 * 10, errorCode = 31212101)
    @Log(module = "非开放性报名管理", business = "批量选择考生校验")
    @PostMapping(value = "/batchSelectStudentCheck")
    public void batchSelectStudentCheck(@Validated(value = Check.class) @RequestBody BatchSelectStudentSaveParam batchSelectStudentSaveParam,
                                        BindingResult bindingResult) {
        this.checkValid(bindingResult);
        BaseCheckEntity baseCheckEntity = iNonOpenService.batchSelectStudentCheck(batchSelectStudentSaveParam);
        ProgressManager.finish(baseCheckEntity);
    }

    @ApiOperation(value = "21.2.2 批量选择考生保存", notes = "20000:成功")
    @ApiOperationSort(value = 7)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ProgressAnno(timeoutSeconds = 60 * 10, errorCode = 31212201)
    @Log(module = "非开放性报名管理", business = "批量选择考生保存")
    @PostMapping(value = "/batchSelectStudentSave")
    public void batchSelectStudentSave(@Validated(value = Insert.class) @RequestBody BatchSelectStudentSaveParam batchSelectStudentSaveParam,
                                       BindingResult bindingResult) {
        this.checkValid(bindingResult);
        // 设置用户ID
        Optional.ofNullable(this.getUserId())
                .ifPresent(batchSelectStudentSaveParam::setUserId);
        iNonOpenService.batchSelectStudentSave(batchSelectStudentSaveParam);
    }

    @ApiOperation(value = "21.2.3 获取报名平台考生", notes = "20000:成功")
    @ApiOperationSort(value = 8)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31212301, print = "获取报名平台考生失败")
    @Log(module = "非开放性报名管理", business = "获取报名平台考生")
    @PostMapping(value = "/getStudentBaseIds")
    public ApiResponse<List<String>> getStudentBaseIds(@ApiParam("报名ID") @RequestParam String projectId) {
        return ApiResponse.<List<String>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iNonOpenService.getStudentBaseIds(projectId))
                .build();
    }

    @ApiOperation(value = "21.3.1 批量导入数据校验",
            notes = "20000:成功",
            response = ImportStudentQualificationsCheckVO.class)
    @ApiOperationSort(value = 9)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ProgressAnno(timeoutSeconds = 60 * 10, errorCode = 31213101)
    @Log(module = "非开放性报名管理", business = "批量导入数据校验")
    @RequestMapping(value = "/importStudentQualificationsCheck", method = RequestMethod.POST)
    public void importStudentQualificationsCheck(@Validated @RequestBody ImportStudentQualificationsCheckParam param,
                                                 BindingResult bindingResult) {
        this.checkValid(bindingResult);
        BaseCheckEntity importStudentQualificationsCheckVO = iNonOpenService.importStudentQualificationsCheck(param);
        ProgressManager.finish(importStudentQualificationsCheckVO);
    }

    @ApiOperation(value = "21.3.2 批量导入数据保存", notes = "20000:成功")
    @ApiOperationSort(value = 10)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ProgressAnno(timeoutSeconds = 60 * 10, errorCode = 31213201)
    @Log(module = "非开放性报名管理", business = "批量导入数据保存")
    @PostMapping(value = "/importStudentQualificationsData")
    public void importStudentQualificationsData(@Validated @RequestBody ImportStudentQualificationsDataParam param,
                                                BindingResult bindingResult) {
        this.checkValid(bindingResult);

        Optional.ofNullable(getOfficeId())
                .ifPresent(param::setOfficeId);
        Optional.ofNullable(getDeptId())
                .ifPresent(param::setDeptId);
        Optional.ofNullable(getUserId())
                .ifPresent(param::setUserId);

        iNonOpenService.importStudentQualificationsData(param);
    }

    @ApiOperation(value = "21.4.1 批量设置科目", notes = "20000:成功")
    @ApiOperationSort(value = 11)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31214101, print = "批量设置科目失败")
    @Log(module = "非开放性报名管理", business = "批量设置科目")
    @PostMapping(value = "/batchSubjectSetting")
    public ApiResponse<?> batchSubjectSetting(@Validated @RequestBody BatchSubjectSettingParam param,
                                              BindingResult bindingResult) {
        this.checkValid(bindingResult);

        Optional.ofNullable(getUserId())
                .ifPresent(param::setUserId);

        iNonOpenService.batchSubjectSetting(param);
        return ApiResponse.builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .build();
    }

    @ApiOperation(value = "21.12.1 导出报名资格", notes = "20000:成功")
    @ApiOperationSort(value = 12)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @Log(module = "非开放性报名管理", business = "导出报名资格")
    @ProgressAnno(total = 100, timeoutSeconds = 60 * 10, errorCode = 31214102, canConcurrent = true)
    @GetMapping("/export")
    public void export(NoOpenQueryParam param) {
        try{
            iNonOpenService.exportNonOpenInfo(param);
        } catch (Exception ex){
            throw new KqException(31214102,"导出报名资格失败");
        }

    }

}
